/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.login;

import assignment_cp3120.ucm.UC2AL;
import assignment_cp3120.ucm.UCLoginUser;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Richard Irons
 */
public class LoginView extends JFrame {
    //used to set the length of the textfield
    private final static String userFieldParse = "mmmmmmmmmm";
    private final static String passFieldParse = "mmmmmmmmmm";
    
    /**
     * exit type, the main window will be the controlling frame and this login
     * window will be hiden, not application terminated
     */
    public final static int EXIT_TYPE = JFrame.DO_NOTHING_ON_CLOSE;
    
    //objects on frame
    private JLabel usernameLabel;      
    private JTextField usernameField;      
    private JLabel passwordLabel;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JButton cancelButton;
    private JLabel loginTypeLabel;
    private JComboBox loginTypeComBox;
    
    private static final String[] loginTypeStr = {"User", "Manager", "Admin"};
    
    private LoginModel model;

    private static LoginView instance;
    /**
     * Default constructor, calls Constructor(model) with a new model
     */
    public LoginView(){
        this(new LoginModel());
    }
    public static LoginView getInstance(){
        if(instance == null){
            instance = new LoginView();
        }
        return instance;
    }
    /**
     * Constructor that sets the model to the input. Also creates the login frame
     * @param model
     */
    public LoginView(LoginModel model) {
        instance = this;
        this.model = model;
        //layout
        GridLayout grid = new GridLayout(4,2);
        setLayout(grid);
        
        //create buttons, labels etc
        usernameLabel = new JLabel("Enter Username:");
        usernameLabel.setHorizontalAlignment(SwingConstants.LEFT);        

        usernameField = new JTextField();        
        setFieldWidth(usernameField,userFieldParse);
              
        
        passwordLabel = new JLabel("Enter Password:");
        passwordLabel.setHorizontalAlignment(SwingConstants.LEFT);            
        
        passwordField = new JPasswordField();
        setFieldWidth(passwordField,passFieldParse);
        
        
        loginTypeLabel = new JLabel("Login Type");        
        loginTypeComBox = new JComboBox(loginTypeStr);
        
        JPanel loginButtonPanel = new JPanel(new FlowLayout());
        
        loginButton = new JButton("Login");       
        loginButtonPanel.add(loginButton);
        
        cancelButton = new JButton("Cancel");
        loginButtonPanel.add(cancelButton);
        
        //first row username lable and field
        add(usernameLabel); add(usernameField);
        //second row password lable and field
        add(passwordLabel); add(passwordField);
        //thrid row login type and combobox
        add(loginTypeLabel); add(loginTypeComBox);
        //fourth row blank pannel and login button panel
        add(new JPanel()); add(loginButtonPanel);
        
        //display        
        setTitle("Login");
        setDefaultCloseOperation(EXIT_TYPE);
        setAlwaysOnTop(true);
        setResizable(false);
        pack();
        setLocationRelativeTo(null); //center frame
        
        cancelButton.addActionListener(new CancelButtonAL());        
        loginButton.addActionListener(new UC2AL(new UCLoginUser(this, model)));
    }

    /**
     * Shows the GUI and sets running to true
     */
    public void showGUI(){
        setVisible(true);
    }
    /**
     * Hides the window and sets running to false.
     */
    public void hideGUI(){
        clearUsernamePasswordField();
        setVisible(false);
    }
    /**
     * Add action commands and the inputed action listener to the buttons
     * @param a
     * Action listener to add to the button
     */
    private class CancelButtonAL implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            hideGUI();
            canceledMessageBox();
            clearUsernamePasswordField();
        }       
    }
    /**
     * Clear the username and password fields
     */
    public void clearUsernamePasswordField(){
        usernameField.setText("");
        passwordField.setText("");
    }
    /**
     * Get the text from the username field
     * @return
     * Text in the username field
     */
    public String getUsernameField(){return usernameField.getText();}
    /**
     * Get the text from the password field
     * @return
     * Password in the password field as a char[]
     */
    public char[] getPasswordField(){return passwordField.getPassword();}
    /**
     * Get the user type from the login combo box
     * @return
     * The selected login type in the combo box as a string
     */
    public String getUserType(){return loginTypeComBox.getSelectedItem().toString();}
    /**
     * Create a message dialog
     * @param parentcomponent
     * @param message
     * Message as a string to be show
     * @param title
     * Title of the message dialog box
     * @param messageType
     * The message type
     */
    public void message(Component parentcomponent,
            Object message,
            String title,
            int messageType ){            
        JOptionPane.showMessageDialog(parentcomponent,message,title,messageType);
    }
    /**
     * Use the message function to create an invaild username and pass error message
     */
    public void errorMessageBox(){
        message(this,"Invalid username or password",
        "Invalid User",
        JOptionPane.ERROR_MESSAGE);
    }
    /**
     * Use the message function to create a login successfull message box
     */
    public void successMessageBox(){
        message(this,"Login was successful",
        "Logged In",
        JOptionPane.INFORMATION_MESSAGE);
    }
    /**
     * Use the message function to create a canceled message boc
     */
    public void canceledMessageBox(){
        message(this,"Login canceled",
        "Canceled",
        JOptionPane.WARNING_MESSAGE);
    }
    /**
     * Used to create the length of the text. Takes in the field to be set and a string of characters the set the length to.
     * Idealy a string of 'm''s would be best as m is the widest character in most fonts.
     * This sets the text field to the string, sets the prefered size to that size then clears the textfield
     * @param field
     * @param length
     */
    public void setFieldWidth(JTextField field, String length){
        field.setText(length);
        field.setPreferredSize(field.getPreferredSize());
        field.setText("");
    }
}